//
//  BankHeadView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/25.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol BankHeadViewDelegate {
    @objc optional func selectBank(type:String,name:String) //选择银行
}

class BankHeadView: UIView {
    
    weak var bankDelegate : BankHeadViewDelegate?
    
    fileprivate var bankType = businessHall //银行类型
    fileprivate var bankName = allBanks //银行名称
    fileprivate var currentTag = 0
    
    fileprivate let cellId = "bankCell"
    var dataArray = [String]()
    
    var tableView : UITableView!
    
    fileprivate let leftButton = BankButton.init(title: businessHall)
    fileprivate let rightButton = BankButton.init(title: allBanks)

    init() {
        super.init(frame: .zero)
        
        let topView = UIView()
        topView.backgroundColor = UIColor.mineColor()
        self.addSubview(topView)
        
        leftButton.tag = 1
        leftButton.addTarget(self, action: #selector(selectBank(sender:)), for: .touchUpInside)
        topView.addSubview(leftButton)
        rightButton.tag = 2
        rightButton.addTarget(self, action: #selector(selectBank(sender:)), for: .touchUpInside)
        topView.addSubview(rightButton)
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        tableView.isHidden = true
        tableView.separatorStyle = .none
        tableView.tableFooterView = UIView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(NearBankCell.self, forCellReuseIdentifier: cellId)
        self.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(topView.snp.bottom)
        }
        
        topView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 60))
        }
        
        leftButton.snp.makeConstraints { (make) in
            make.left.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.width.equalTo(ScreenW/2)
        }
        
        rightButton.snp.makeConstraints { (make) in
            make.right.bottom.equalToSuperview()
            make.top.equalTo(leftButton)
            make.width.equalTo(leftButton)
        }
    }
    
    //MARK: 选择银行
    @objc fileprivate func selectBank(sender:BankButton) {
        
        sender.image = sender.image == #imageLiteral(resourceName: "up_arrow") ? #imageLiteral(resourceName: "bottom_arrow") : #imageLiteral(resourceName: "up_arrow")
        self.currentTag = sender.tag
        
        if sender.tag == 1 {
            rightButton.image = #imageLiteral(resourceName: "bottom_arrow")
            self.dataArray = [businessHall,selfServiceBank]
            self.tableView.reloadData()
        }else{
            leftButton.image = #imageLiteral(resourceName: "bottom_arrow")
            
            let plistPath = Bundle.main.path(forResource: "banks", ofType: "plist")
            let array = NSArray.init(contentsOfFile: plistPath!)
            self.dataArray = array as! [String]
            self.tableView.reloadData()
        }
        
        if leftButton.image == #imageLiteral(resourceName: "bottom_arrow") && rightButton.image == #imageLiteral(resourceName: "bottom_arrow") {
            self.tableView.isHidden = true
        }else{
            self.tableView.isHidden = false
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension BankHeadView : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 50)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? NearBankCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        if self.currentTag == 1 {
            self.bankType = dataArray[indexPath.row]
            leftButton.title = dataArray[indexPath.row]
            
        }else{
            self.bankName = dataArray[indexPath.row]
            rightButton.title = dataArray[indexPath.row]
        }
        
        self.bankDelegate?.selectBank!(type: self.bankType, name: self.bankName)
        self.tableView.isHidden = true
        leftButton.image = #imageLiteral(resourceName: "bottom_arrow")
        rightButton.image = #imageLiteral(resourceName: "bottom_arrow")
    }
}
